import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/game_medal_rank_model.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/domain/games/game_utils/game_utils_controller.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/widgets/normal_table.dart';

class WardListHome extends StatefulWidget {
  @override
  _WardListHomeState createState() => _WardListHomeState();
}

class _WardListHomeState extends State<WardListHome> {
  bool isLogin = UserInfo.me.isLogin;
  List<WardData> wardsData = [];
  bool isDispose = false;
  NormalTableViewController controller = NormalTableViewController();
  @override
  void dispose() {
    isDispose = true;
    super.dispose();
  }

  @override
  void initState() {
    // _initInfo();
    super.initState();
    GameUtilsController.managerCont['3'] = controller;
  }

  // Future _initInfo() async {
  //   gameMedalRankList
  //   var data = await API.shared.game.wards();
  //   if (!isDispose) {
  //     setState(() {
  //       wardsData = data;
  //     });
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    // NormalGridView
    return Scaffold(
      body: Container(
        child: Column(
          children: [
            Container(
              height: 64,
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.cover,
                  image: AssetImage('assets/images/ward_header.png'),
                ),
              ),
            ),
            Container(
              height: 48,
              decoration: BoxDecoration(
                color: Color(0x33FDD000),
              ),
              child: Row(
                children: [
                  Expanded(
                    flex: 50,
                    child: Padding(
                        padding: EdgeInsets.only(left: 14),
                        child: Row(
                          children: [
                            Text(
                              S.of(context).rank,
                              style: TextStyle(
                                color: ThemeColor.colorBlack100,
                                fontSize: 12,
                                fontWeight: FontWeight.w400,
                                fontFamily: 'PingFang SC',
                              ),
                            ),
                            SizedBox(width: 14),
                            Text(
                              S.of(context).Country_Region,
                              style: TextStyle(
                                color: ThemeColor.colorBlack100,
                                fontSize: 12,
                                fontWeight: FontWeight.w400,
                                fontFamily: 'PingFang SC',
                              ),
                            ),
                          ],
                        )),
                  ),
                  Expanded(
                      flex: 50,
                      child: Padding(
                        padding: EdgeInsets.only(right: 15),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Image.asset(
                              'assets/images/gold.png',
                              width: 25,
                              height: 25,
                            ),
                            Image.asset(
                              'assets/images/sliver.png',
                              width: 25,
                              height: 25,
                            ),
                            Image.asset(
                              'assets/images/bronze.png',
                              width: 25,
                              height: 25,
                            ),
                            Container(
                              child: Text(
                                S.of(context).Medal_in_Total,
                                style: TextStyle(
                                  fontWeight: FontWeight.w400,
                                  color: ThemeColor.colorBlack100,
                                  fontSize: 12,
                                  fontFamily: 'PingFang SC',
                                ),
                                textAlign: TextAlign.center,
                              ),
                              width: 50,
                            )
                          ],
                        ),
                      )),
                ],
              ),
            ),
            Expanded(
                child: NormalTableView<GameMedalRankModel>(
              enableRefresh: false,
              enableLoadMore: false,
              controller: controller,
              itemBuilder: (p0, p1) {
                return listItem(p1);
              },
              getData: (page) async {
                var json =
                    await API.shared.game.gameMedalRankList(page, size: 300);
                return json;
              },
            ))
          ],
        ),
        //       var body = NormalTableView<VenueItem>(
        // enableRefresh: true,
        // enableLoadMore: false,
        // padding: EdgeInsets.symmetric(horizontal: 15, vertical: 20),
        //   itemBuilder: (ctx, item) {
        //     var entity = VenueEntity(item.cover, item.name, item.address);
        //     return GestureDetector(
        //       child: Container(
        //         child: VenueItemCell(entity),
        //         padding: EdgeInsets.only(bottom: 10),
        //       ),
        //       onTap: () => RouteManager.push(
        //         context,
        //         RoutMapping.venueDetailPage(item.id),
        //       ),
        //     );
        //   },
        //   getData: (page) async {
        //     var json = await API.shared.venue.list();
        //     return json;
        //   },
        // );
        // child: ListView(
        //   children: [
        //     Container(
        //       height: 64,
        //       decoration: BoxDecoration(
        //         image: DecorationImage(
        //           fit: BoxFit.cover,
        //           image: AssetImage('assets/images/ward_header.png'),
        //         ),
        //       ),
        //     ),
        //     Container(
        //       height: 48,
        //       decoration: BoxDecoration(
        //         color: Color(0x33FDD000),
        //       ),
        //       child: Row(
        //         children: [
        //           Expanded(
        //             flex: 50,
        //             child: Padding(
        //                 padding: EdgeInsets.only(left: 14),
        //                 child: Row(
        //                   children: [
        //                     Text(
        //                       S.of(context).rank,
        //                       style: TextStyle(
        //                         color: ThemeColor.colorBlack100,
        //                         fontSize: 12,
        //                         fontWeight: FontWeight.w400,
        //                         fontFamily: 'PingFang SC',
        //                       ),
        //                     ),
        //                     SizedBox(width: 14),
        //                     Text(
        //                       S.of(context).Country_Region,
        //                       style: TextStyle(
        //                         color: ThemeColor.colorBlack100,
        //                         fontSize: 12,
        //                         fontWeight: FontWeight.w400,
        //                         fontFamily: 'PingFang SC',
        //                       ),
        //                     ),
        //                   ],
        //                 )),
        //           ),
        //           Expanded(
        //               flex: 50,
        //               child: Padding(
        //                 padding: EdgeInsets.only(right: 15),
        //                 child: Row(
        //                   mainAxisAlignment: MainAxisAlignment.spaceBetween,
        //                   children: [
        //                     Image.asset(
        //                       'assets/images/gold.png',
        //                       width: 25,
        //                       height: 25,
        //                     ),
        //                     Image.asset(
        //                       'assets/images/sliver.png',
        //                       width: 25,
        //                       height: 25,
        //                     ),
        //                     Image.asset(
        //                       'assets/images/bronze.png',
        //                       width: 25,
        //                       height: 25,
        //                     ),
        //                     Container(
        //                       child: Text(
        //                         S.of(context).Medal_in_Total,
        //                         style: TextStyle(
        //                           fontWeight: FontWeight.w400,
        //                           color: ThemeColor.colorBlack100,
        //                           fontSize: 12,
        //                           fontFamily: 'PingFang SC',
        //                         ),
        //                         textAlign: TextAlign.center,
        //                       ),
        //                       width: 50,
        //                     )
        //                   ],
        //                 ),
        //               )),
        //         ],
        //       ),
        //     ),
        //     ...wardsData.map((e) => listItem(e)).toList(),
        //   ],
        // ),
      ),
    );
  }

  Widget listItem(GameMedalRankModel model) {
    //, int index
    // var index = 0;
    // try {
    //   index = wardsData.indexOf(data);
    // } catch (e) {
    //   index = 0;
    // }
    // index += 1;
    return Container(
      padding: EdgeInsets.symmetric(vertical: 14),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(width: 1, color: Color(0x19000000)),
        ),
      ),
      alignment: Alignment.center,
      child: Row(
        children: [
          Expanded(
            flex: 49,
            child: Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: [
                    Container(
                      child: AutoSizeText(
                        (model.rankequal == 'Y' ? '=' : '') +
                            (model.rank ?? '').toString(),
                        style: TextStyle(
                          color: ThemeColor.colorBlack100,
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                        maxFontSize: 12,
                        maxLines: 1,
                        textAlign: TextAlign.center,
                      ),
                      width: 25,
                    ),
                    SizedBox(width: 15),
                    // Container(
                    //   width: 35,
                    //   height: 28,
                    //   decoration: BoxDecoration(
                    //       border: Border.all(
                    //           color: Colors.black.withOpacity(0.1),
                    //           width: 0.5)),
                    //   child: Image.network(
                    //     model.nationalFlag ?? '',
                    //     fit: BoxFit.contain,
                    //   ),
                    // ),
                    // SizedBox(width: 8),
                    Expanded(
                      child: Text(
                        model.name ?? '',
                        style: TextStyle(
                          color: ThemeColor.colorBlack100,
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          fontFamily: 'PingFang SC',
                        ),
                        maxLines: 5,
                      ),
                    ),
                    SizedBox(width: 8),
                  ],
                )),
          ),
          Expanded(
              flex: 50,
              child: Padding(
                padding: EdgeInsets.only(right: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      width: 25,
                      child: Text(
                        '${model.gold}',
                        style: TextStyle(
                          color: Color(0xffF7B004),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    Container(
                      width: 25,
                      child: Text(
                        "${model.silver}",
                        style: TextStyle(
                          color: Color(0xff8D9DAC),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    Container(
                      width: 25,
                      child: Text(
                        "${model.bronze}",
                        style: TextStyle(
                          color: Color(0xff996530),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    Container(
                      child: Text(
                        "${model.total}",
                        style: TextStyle(
                          color: Color(0xff111111),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                        ),
                        textAlign: TextAlign.center,
                      ),
                      width: 50,
                    )
                  ],
                ),
              )),
        ],
      ),
    );
  }
}

class WardData {
  late String org;
  late String national_flag;
  late int gold;
  late int silver;
  late int bronze;
  late int total;
  late String name;

  WardData(Map<String, dynamic> json) {
    org = "${json["org"]}";
    national_flag = "${json["national_flag"]}";
    name = "${json["name"]}";
    try {
      gold = json['gold'] as int;
    } catch (e) {
      gold = 0;
    }
    try {
      silver = json['silver'] as int;
    } catch (e) {
      silver = 0;
    }
    try {
      bronze = json['bronze'] as int;
    } catch (e) {
      bronze = 0;
    }
    try {
      total = json['total'] as int;
    } catch (e) {
      total = 0;
    }
  }
}
